package asc.common.util;

//base class
import java.util.Enumeration;
import java.util.Hashtable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import asc.common.util.StringUtil;

public class PageSplit{
	protected static Log logger = LogFactory.getLog(PageSplit.class);
	
	private static String defaultTemplate = null;
	
	static {
		StringBuffer defaultTemplateBuff = new StringBuffer();
//		defaultTemplateBuff.append("<div class=\"paging\">");

		defaultTemplateBuff.append("<ul>");
		defaultTemplateBuff.append("<!--FRONT_START-->\n");
		defaultTemplateBuff.append("<li><!--FIRST_LINK_IMG--></li>\n");
		defaultTemplateBuff.append("<li style=\"padding-right:3px;\"><!--PREV_LINK_IMG--></li>");
		defaultTemplateBuff.append("<!--FRONT_END-->");
		
		defaultTemplateBuff.append("<!--LOOP_START-->");
		defaultTemplateBuff.append("<!--PAGE_LINK--><!--PAGE_NUMBER-->"); 
		defaultTemplateBuff.append("<!--LOOP_END-->");

		defaultTemplateBuff.append("<!--BACK_START-->");
		defaultTemplateBuff.append("\n<li style=\"padding-right:3px;\"><!--NEXT_LINK_IMG--></li>\n");
		defaultTemplateBuff.append("<li><!--LAST_LINK_IMG--></li>");
		defaultTemplateBuff.append("<!--BACK_END-->\n");
		defaultTemplateBuff.append("</ul>");
		
		defaultTemplate = defaultTemplateBuff.toString();
	}

	public String getSplitPageLink(int total, int page, int scale, int pageScale, String url) {

		// check template and default splitLinkString
		String linkHtmlContent = defaultTemplate;

		// basic info
		if(total == 0)	{total = 1;}
		if(total <= scale*(page-1)){page=(total/scale)+1;}
		
		int totalPageCount = getTotalPageCount(total, scale);
		int startPage = getFirstPage(totalPageCount, page, pageScale);
		int repeatCount = getRepeatCount(totalPageCount, page, pageScale);	
		int endPage = startPage + repeatCount;
		
		int previousPage = getPreviousPage(totalPageCount, page, pageScale);
		int nextPage = getNextPage(totalPageCount, page, pageScale);

		// make one line string
		//linkHtmlContent = StringUtil.replaceByPattern(linkHtmlContent, "\n", "", true);

		// replace first, prev, next, last Factor
		Hashtable partsHash = new Hashtable();
		String pageDelim = ((url).indexOf("?") >= 0) ? "&" : "?"; 
		
		if (isFirstPageSet(totalPageCount, page, pageScale)) {
			// remove front icons (first icon and prev icon)
			//linkHtmlContent = StringUtils.replaceByPattern(linkHtmlContent, "<!--FRONT_START-->.*?<!--FRONT_END-->", "", true);
			partsHash.put("FIRST_LINK_IMG", "<img src=\"/cmc/images/button/btn_list_first_off.gif\" title=\"ó��\">");
			partsHash.put("PREV_LINK_IMG", "<img src=\"/cmc/images/button/btn_list_prev_off.gif\" title=\"����\">");
//			partsHash.put("FIRST_LINK", "href=\"#\" title=\"" + 1 + "\"");
//			partsHash.put("PREV_LINK" , "href=\"#\"");
		} else {
			partsHash.put("FIRST_LINK_IMG", "<img onClick=\"USR_GOPAGE('1');\" src=\"/cmc/images/button/btn_list_first_on.gif\" title=\"ó��\">");
			partsHash.put("PREV_LINK_IMG",  "<img onClick=\"USR_GOPAGE('" + previousPage + "');\" src=\"/cmc/images/button/btn_list_prev_on.gif\" title=\"����\">");
//			partsHash.put("FIRST_LINK", "href=\"javascript:USR_GOPAGE('1');\" title=\"" + 1 + "\"");
//			partsHash.put("PREV_LINK" , "href=\"javascript:USR_GOPAGE('" + previousPage + "');\"");
		}
		
		if (isLastPageSet(totalPageCount, page, pageScale)) {
			// remove back icons (next icon And last icon)
			//linkHtmlContent = StringUtils.replaceByPattern(linkHtmlContent, "<!--BACK_START-->.*?<!--BACK_END-->", "", true);
			partsHash.put("NEXT_LINK_IMG" , "<img src=\"/cmc/images/button/btn_list_next_off.gif\" title=\"����\">");
			partsHash.put("LAST_LINK_IMG" , "<img src=\"/cmc/images/button/btn_list_last_off.gif\" title=\"������\">");
//			partsHash.put("NEXT_LINK" , "href=\"#\"");
//			partsHash.put("LAST_LINK" , "href=\"#\" title=\"" + totalPageCount + "\"");			
		} else {
			partsHash.put("NEXT_LINK_IMG" , "<img onClick=\"USR_GOPAGE('" + nextPage + "');\" src=\"/cmc/images/button/btn_list_next_on.gif\" title=\"����\">");
			partsHash.put("LAST_LINK_IMG" , "<img onClick=\"USR_GOPAGE('" + totalPageCount + "');\" src=\"/cmc/images/button/btn_list_last_on.gif\" title=\"������\">");
//			partsHash.put("NEXT_LINK" , "href=\"javascript:USR_GOPAGE('" + nextPage + "');\"");
//			partsHash.put("LAST_LINK" , "href=\"javascript:USR_GOPAGE('" + totalPageCount + "');\" title=\"" + totalPageCount + "\"");		
		}

		Enumeration en = partsHash.keys();
		while (en.hasMoreElements()) {
			String key = (String)en.nextElement();
			linkHtmlContent = StringUtil.replaceByPattern(linkHtmlContent, "<!--" + key + "-->", (String)partsHash.get(key), true);
		}
		
		// cut number line template
		String[][] delimArray = {
			{"<!--LOOP_START-->" , "<!--PAGE_LINK-->"},
			{"<!--PAGE_LINK-->"  , "<!--PAGE_NUMBER-->"},
			{"<!--PAGE_NUMBER-->", "<!--LOOP_END-->"}
		};
		String[] templateParts = new String[delimArray.length];
		
		StringBuffer numberLinkBuff = new StringBuffer();
		for (int i=0; i < delimArray.length; i++) {
			int startDelimIndex = defaultTemplate.indexOf(delimArray[i][0]);
			int endDelimIndex   = defaultTemplate.indexOf(delimArray[i][1]);
			if (startDelimIndex >= 0 && endDelimIndex >= 0) { // make parts
				templateParts[i] = defaultTemplate.substring(startDelimIndex  + delimArray[i][0].length(), endDelimIndex);
			}
		}

		for (int i=startPage; i < endPage; i++) {
			numberLinkBuff.append(templateParts[0]);
			if (i == page) {
				numberLinkBuff.append("\n<li>");
				numberLinkBuff.append("<a href=\"#\" class=\"current_page\">");
				numberLinkBuff.append(templateParts[1]);
				numberLinkBuff.append(""+i+"");
				numberLinkBuff.append("</a>");
				numberLinkBuff.append("</li>");
				numberLinkBuff.append(templateParts[2]);
			} else {
				numberLinkBuff.append("\n<li>");
				numberLinkBuff.append("<a href=\"#\" onClick=\"USR_GOPAGE('" + i + "');\" >" );
				numberLinkBuff.append(templateParts[1]);
				numberLinkBuff.append(""+i);
				numberLinkBuff.append("</a>");
				numberLinkBuff.append("</li>");
				numberLinkBuff.append(templateParts[2]);
			}

		}
		
		String loopStart = "<!--LOOP_START-->";
		String loopEnd   = "<!--LOOP_END-->";
		linkHtmlContent = StringUtil.replaceByPattern(linkHtmlContent, loopStart + ".*?" + loopEnd, numberLinkBuff.toString(), true);

		return linkHtmlContent;
	}

	public int getTotalPageCount(int total, int scale) {
		int remainder = total % scale;
		int quotient = total / scale;
		
		return (remainder > 0) ? quotient + 1 : quotient; 
	}

	public int getPageRemainder(int totalPageCount, int pageScale) {
		int remainder = totalPageCount % pageScale;
		return remainder; 
	}

	public int getPageQuotient(int totalPageCount, int pageScale) {
		int quotient = totalPageCount / pageScale;
		
		return quotient; 
	}

	public int getFirstPage(int totalPageCount, int page, int pageScale) {
		int remainder = page % pageScale;
		int quotient = page / pageScale;
		
		return (remainder > 0) ? (quotient * pageScale)+1 : ((quotient-1) * pageScale)+1; 
	}

	public int getRepeatCount(int totalPageCount, int page, int pageScale) {
		int repeatCount = -1;
		// ���� pageSet �� ù��° ��ȣ
		if (isLastPageSet(totalPageCount, page, pageScale)) {
			repeatCount = (getPageRemainder(totalPageCount, pageScale) == 0) ? pageScale : getPageRemainder(totalPageCount, pageScale);
		} else {
			repeatCount = pageScale;	
		}
		
		return repeatCount;
	}

	public int getPreviousPage(int totalPageCount, int page, int pageScale) {
		int previousPage = -1;
		if (getCurrentPageSet(page, pageScale) > 1) {
			previousPage = getFirstPage(totalPageCount, page, pageScale) - 1;
		}
		return previousPage;
	}

	public int getNextPage(int totalPageCount, int page, int pageScale) {
		int nextPage = -1;
		
		if (!isLastPageSet(totalPageCount, page, pageScale)) {
			nextPage = getFirstPage(totalPageCount, page, pageScale) + pageScale;	
		}
		return nextPage;
	}

	public int getCurrentPageSet(int page, int pageScale) {
		int remainder = page % pageScale;
		int quotient = page / pageScale;
		
		return (remainder > 0) ? quotient + 1 : quotient; 
	}

	public int getTotalPageSetCount(int totalPageCount, int pageScale) {
		int remainder = totalPageCount % pageScale;
		int quotient = totalPageCount / pageScale;
		
		return (remainder > 0) ? quotient + 1 : quotient; 
	}

	public boolean isFirstPageSet(int totalPageCount, int page, int pageScale) {
		boolean firstPageSetFlag = false;
		
		if (getFirstPage(totalPageCount, page, pageScale) == 1) {
			firstPageSetFlag = true;	
		}	
		return firstPageSetFlag;
	}

	public boolean isLastPageSet(int totalPageCount,  int page, int pageScale) {
		boolean lastPageSetFlag = false;
		
		int currentPageSet = getCurrentPageSet(page, pageScale);
		int totalPageSetCount = getTotalPageSetCount(totalPageCount, pageScale);
		if (currentPageSet == totalPageSetCount) {
			lastPageSetFlag = true;		
		}
		return lastPageSetFlag;
	}
}